package com.future.hedongjun.home.utils;

import android.content.Context;
import android.text.TextUtils;

import com.future.baselib.utils.BasePreference;

import java.util.Arrays;
import java.util.List;

/**
 * @author jniu
 * @Email niuxiaoqi@hztianque.com
 * @time 2018/12/26
 */
class HistorySpHelper extends BasePreference {

    private int HistoryLength = 10;
    private String spiltStr = ",";
    private String key = "search_history";

    protected HistorySpHelper(Context context) {
        super(context, "searchHistory");
    }

    public void addHistory(String txt) {
        String searchHistory = getString(key);
//            searchHistory.split(spiltStr)
        if (TextUtils.isEmpty(searchHistory)) {
            searchHistory = txt;
        } else {
            searchHistory = searchHistory + spiltStr + txt;
            if (searchHistory.split(spiltStr).length > HistoryLength) {
                searchHistory = searchHistory.substring(searchHistory.indexOf(spiltStr) + 1);
            }
        }
        setString(key, searchHistory);
    }

    public List<String> getHistory() {
        String string = getString(key);
        if (TextUtils.isEmpty(string)) {
            return null;
        }
        String[] split = string.split(spiltStr);
        List<String> stringList = Arrays.asList(split);
        return stringList;
    }

    public void clearHistory() {
        setString(key, "");
    }

}
